package com.ruoyi.task.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.task.domain.dto.FileDTO;
import com.ruoyi.task.service.UploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
public class UploadController extends BaseController {

    private final UploadService uploadService;
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {

        }
        return uploadService.upload(file);
    }

    @PostMapping("/delete")
    public AjaxResult deleteFile(@RequestBody FileDTO file){
        //http://101.43.32.126:9000/powerproject/file/2024/10/10/8f325f4772a145b0b8e3a8c0201a8ebb.mp3
        //要携带路径 这个参数不能放在get请求里面 需要放在post请求中去
        String url=file.getUrl();
        String path=url.substring(url.indexOf("file"));
        System.out.println(path);
        return toAjax(uploadService.delete(path));
    }
}
